<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/5/8
 * Time: 下午1:20
 */

namespace App\Http\Controllers;

use App\Http\Models\Brand;
use App\Http\Models\Category;
use App\Http\Models\Language;
use App\Http\Models\Product;
use App\Http\Presenter\ProductPresenter;
use App\Http\Repository\ProductRepository;
use App\Http\Service\ProductService;
use Illuminate\Http\Request as HttpRequest;

class ProductController extends BaseController
{

    /**
     * @param ProductPresenter $presenter
     * @param ProductRepository $repository
     * @param ProductService $service
     */
    public function __construct(ProductPresenter $presenter, ProductRepository $repository, ProductService $service)
    {

        $this->view = ['activeFirst' => 'product', 'activeSecond' => 'index'];

        parent::__construct($presenter, $repository, $service);
    }

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {

        $productList = Product::all(['id', 'short', 'created_at', 'updated_at']);

        if (count($productList)) {

            $productList->load('brand');
        }

        return view('product.index', array_merge(['productList' => $productList], $this->view));
    }

    public function create()
    {

        $brandList = Brand::get(['id', 'name']);

        $languageList = Language::get(['id', 'title']);

        $product = new Product();

        $this->view['activeSecond'] = 'create';

        return view('product.edit', array_merge(['brandList' => $brandList, 'languageList' => $languageList, 'product' => $product], $this->view));
    }

    public function update($id)
    {

        $brandList = Brand::get(['id', 'name']);

        $languageList = Language::get(['id', 'title']);

        $product = Product::query()->find($id);

        $this->view['activeSecond'] = 'index';

        return view('product.edit', array_merge(['brandList' => $brandList, 'languageList' => $languageList, 'product' => $product], $this->view));
    }

    public function save(HttpRequest $request)
    {

        $productArray = $request->all();

        if (empty($productArray['id'])) {

            $product = new Product();


        } else {

            $product = Product::query()->find($productArray['id']);
        }

        $product->brand_id = empty($productArray['brand_id']) ? '' : $productArray['brand_id'];

        $product->title = empty($productArray['title']) ? '' : $productArray['title'];

        $product->short = empty($productArray['short']) ? '' : $productArray['short'];

        $product->poster = empty($productArray['poster']) ? '' : $productArray['poster'];

        $product->desc = empty($productArray['desc']) ? '' : $productArray['desc'];

        $product->language = empty($productArray['language']) ? '' : $productArray['language'];

        $product->save();

        return redirect('product.html');
    }

    public function delete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Product::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function brand()
    {

        $this->view['activeSecond'] = 'brand';

        $brandList = Brand::get(['id', 'category_id', 'name', 'status']);

        return view('product.brand', array_merge(['brandList' => $brandList], $this->view));
    }

    public function brandCreate()
    {

        $categoryList = Category::get(['id', 'name']);

        $languageList = Language::get(['id', 'title']);

        $brand = new Brand();

        $this->view['activeSecond'] = 'brand';

        return view('product.brand-edit', array_merge(['categoryList' => $categoryList, 'languageList' => $languageList, 'brand' => $brand], $this->view));
    }

    public function brandUpdate($id)
    {

        $categoryList = Category::get(['id', 'name']);

        $brand = Brand::query()->find($id);

        $this->view['activeSecond'] = 'brand';

        return view('product.brand-edit', array_merge(['categoryList' => $categoryList, 'brand' => $brand], $this->view));
    }

    public function brandSave(HttpRequest $request)
    {

        $brandArray = $request->all();

        if (empty($brandArray['id'])) {

            $brand = new Brand();

        } else {

            $brand = Brand::query()->find($brandArray['id']);
        }

        $brand->category_id = empty($brandArray['category_id']) ? '' : $brandArray['category_id'];

        $brand->title = empty($brandArray['title']) ? '' : $brandArray['title'];

        $brand->language = empty($brandArray['language']) ? '' : $brandArray['language'];

        $brand->name = empty($brandArray['name']) ? '' : $brandArray['name'];

        $brand->picture = empty($brandArray['picture']) ? '' : $brandArray['picture'];

        $brand->sort = empty($brandArray['sort']) ? '' : $brandArray['sort'];

        $brand->recommend = empty($brandArray['recommend']) ? '' : $brandArray['recommend'];

        $brand->save();

        return redirect('product-brand.html');
    }

    public function brandDelete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Brand::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function category()
    {

        $this->view['activeSecond'] = 'category';

        $categoryList = Category::get(['id', 'name']);

        return view('product.category', array_merge(['categoryList' => $categoryList], $this->view));
    }

    public function categoryCreate()
    {

        $this->view['activeSecond'] = 'category';

        $languageList = Language::get(['id', 'title']);

        $category = new Category();

        return view('product.category-edit', array_merge(['languageList' => $languageList, 'category' => $category], $this->view));
    }

    public function categoryUpdate($id)
    {

        $this->view['activeSecond'] = 'category';

        $languageList = Language::get(['id', 'title']);

        $category = Category::query()->find($id);

        return view('product.category-edit', array_merge(['languageList' => $languageList, 'category' => $category], $this->view));
    }

    public function categorySave(HttpRequest $request)
    {

        $categoryArray = $request->all();

        if (empty($categoryArray['id'])) {

            $category = new Category();

        } else {

            $category = Category::query()->find($categoryArray['id']);
        }

        $category->name = empty($categoryArray['name']) ? '' : $categoryArray['name'];

        $category->tags = empty($categoryArray['tags']) ? '' : $categoryArray['tags'];

        $category->language = empty($categoryArray['language']) ? '' : $categoryArray['language'];

        $category->desc = empty($categoryArray['desc']) ? '' : $categoryArray['desc'];

        $category->save();

        return redirect('product-category.html');
    }

    public function categoryDelete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Category::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function set(HttpRequest $request)
    {

    }

}